package com.synergy.stockflow.procure.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.procure.entity.SupplierPrices;
import com.synergy.stockflow.procure.entity.Vendor;
import com.synergy.stockflow.procure.service.SupplierPricesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/supplierPrices")
public class SupplierPricesController {

    @Autowired
    SupplierPricesService supplierPricesService;

    @GetMapping
    public Result<Page<SupplierPrices>> getAllSupplierPricesController(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(name = "supplierPricesId", required = false) String supplierPricesId,
            @RequestParam(name = "supplierpricesVendorid", required = false) String supplierpricesVendorid,
            @RequestParam(name = "supplierpricesProductsid", required = false) String supplierpricesProductsid){
        Page<SupplierPrices> page = new Page<>(current, size);
        page = supplierPricesService.getSupplierPrices(page, supplierPricesId, supplierpricesVendorid, supplierpricesProductsid);
        return Result.success(page);
    }
    @PostMapping
    public Result insert(@RequestBody SupplierPrices supplierPrices) {
        supplierPrices.setSupplierpricesUpdated(new Date());
        supplierPricesService.save(supplierPrices);
        return Result.success(supplierPrices);
    }

    @PutMapping
    public Result update(@RequestBody SupplierPrices supplierPrices) {
        supplierPricesService.updateById(supplierPrices);
        System.out.println(supplierPrices);
        return Result.success(supplierPrices);
    }
    @DeleteMapping
    public Result delete(@RequestBody List<SupplierPrices> supplierPrices) {
        for(SupplierPrices S : supplierPrices) {
            supplierPricesService.removeById(S.getSupplierPricesid());
        }
        return Result.success(supplierPrices);
    }

    @GetMapping("/getSupplierPrices")
    public Result getSupplierPrices(String supplierpricesProductsid) {
        LambdaQueryWrapper<SupplierPrices> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SupplierPrices::getSupplierpricesProductsid, supplierpricesProductsid);
        return Result.success(supplierPricesService.list(queryWrapper));
    }
}
